/**
 * Copyright (c) 2007, Reliable Source, Inc. 
 * All Rights Reserved.
 */
package org.reliablesource.prism.ui.popup.actions;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Widget;
import org.reliablesource.prism.core.IPrismElement;
import org.reliablesource.prism.core.IPrismScreen;
import org.reliablesource.prism.core.IPrismTutorial;
import org.reliablesource.prism.ui.plugin.PrismPlugin;

/**
 * ExportAsDocumentation.java<br> 
 * Created on: Jul 25, 2007<br>
 *
 * @author Dennis Park 
 * 		<a href="mailto:dennis.park@gmail.com">dennis.park@gmail.com</a>
 *
 * @since 1.0
 *
 */
public class GenerateDocumentationAction extends Action {

	protected IPrismTutorial selection;
	
	/*
	 * (non-Javadoc)
	 * @see org.eclipse.jface.action.Action#run()
	 */
	@Override
	public void run(){
		if(!isEnabled())
			return;
		
		if (selection == null)
			return;
		
		IPrismElement [] screens = selection.getChildren();
		
		int _nElts = screens.length;
		
		StringBuffer _output = new StringBuffer();
		
//		SWT.Show, 
//		SWT.MenuDetect, SWT.Expand, SWT.Collapse, SWT.DragDetect, 
//		SWT.DefaultSelection
		
		for(int i = 0; i < _nElts; i++){
			IPrismElement _current = screens[i];
			if(_current instanceof IPrismScreen){
				
				IPrismScreen _screen = ((IPrismScreen)_current); 
				int _type = _screen.getEventType();
				_output.append(_screen.getName());
				switch(_type){
				case SWT.MenuDetect:
				case SWT.Arm:
					for(; (i + 1 ) < _nElts; i++){
						
						IPrismElement _currentArm = screens[i + 1];
						if(_currentArm instanceof IPrismScreen){
							IPrismScreen _screenArm = ((IPrismScreen)_currentArm); 
							int _armType = _screenArm.getEventType();
							if(_armType != SWT.Arm && _armType != SWT.MenuDetect){
								//	you've reached end.
								if(_armType == SWT.Selection){
									i++;
									if(_output.toString().endsWith("->"))
										_output.append(_screenArm.getName());
									break;
								}
								else if(_armType == SWT.Activate){
									i++;
									if(_output.toString().endsWith("->"))
										_output.append(_screenArm.getName());
									break;
								}
								else if(_armType == SWT.Show){
									_output.append("->");
								}
							}
							else{
								if(_output.toString().endsWith("->"))
									_output.append(_screenArm.getName());
							}
							
						}
					}
					break;
				case SWT.Selection:
					break;
				case SWT.Activate:
					break;
				case SWT.Dispose:
					break;
				case SWT.KeyUp:
					break;
				}
				
				
				_output.append("\t");
				_output.append(PrismPlugin.getDefault().getWidgetType((Widget) _screen.getWidget()));
				
				String type = PrismPlugin.getDefault().getEventLiteral(_type);
				System.err.println(type + "\t --  "+ _output.toString());
				System.err.println();
				
				_output.delete(0, _output.length());
			}
		}
		
	}
	
	public boolean setSelection(final ISelection _selection) {
		setEnabled(false);
		if (_selection != null) {
			
			IStructuredSelection _strSelection = (IStructuredSelection) _selection;

			if(_strSelection.isEmpty())
				return false;
			
			Object _selectedObj = _strSelection.getFirstElement();
			setEnabled(_selectedObj instanceof IPrismTutorial);
					
			if(isEnabled())
				selection = (IPrismTutorial) _selectedObj;
		}
		return isEnabled();
	}
}
